<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><s:if test="vote.voteId==null"><s:text name="vote.add.title"/></s:if><s:else><s:text name="vote.edit.title"/></s:else></title>
	<%@ include file="/common/meta.jsp"%>
	<%@ include file="/common/common.jsp"%>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>

</head>
<body>
<s:set name="siteId" value="#session['CURRENT_SITE'].siteId"></s:set>
<div id="wrapper">
	<div class="curmb"><s:text name="your.site.position"/>:<a href="${ctx}/main.action"><s:text name="homepage"/></a><span>></span><s:text name="vote.title"/></div>
	<div class="listtit"><p><span><strong><s:if test="vote.voteId==null"><s:text name="vote.add.title"/></s:if><s:else><s:text name="vote.edit.title"/></s:else></strong></span></p></div>
	<div class="listadd">
		<s:hidden name="vote.voteTitle" id="votename"/>
		<s:form action="vote-save" onsubmit="return chkform()">
		<s:hidden name="vote.voteId" id="voteId"/>
		<s:hidden name="optArray" id="optarray"/>
		<s:hidden name="ordArray" id="ordArray"/>
		<s:hidden name="optIdArray" id="optIdArray"/>
		<s:hidden name="vote.site.siteId" id="siteid"/>
		<table class="addgrid">
			<tr>
				<th><s:text name="vote.voteTitle"/>:</th>
				<td>
					<s:textfield id="voteTitle" name="vote.voteTitle" required="true" requiredposition="right" cssClass="txtin" onkeyup="check()"/>
					<span id="point-voteTitle-Message" style="color: red;">*</span>
					<span id="voteTitlePointMessage"></span>
				</td>
			</tr>
			<tr>
				<th><s:text name="vote.startTime"/>:</th>
				<%-- <td><sx:datetimepicker name="vote.startTime"  cssClass="txtsearch" displayFormat="yyyy-MM-dd" javascriptTooltip="aasddfasf" /></td> --%>
				<td><input name="vote.startTime" size="30"  class="Wdate" id="qry_startTime_1" readonly="true"  value="<s:date name="vote.startTime" format="yyyy-MM-dd HH:mm:ss"/>"
							onFocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" type="text"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.endTime"/>:</th>
				<%-- <td><sx:datetimepicker name="vote.endTime"  cssClass="txtsearch" displayFormat="yyyy-MM-dd" javascriptTooltip="aasddfasf" /><span id="endtimeno"></span></td> --%>
				<td><input name="vote.endTime" size="30"  class="Wdate" id="qry_startTime_1" readonly="true"  value="<s:date name="vote.endTime" format="yyyy-MM-dd HH:mm:ss"/>"
							onFocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" type="text"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.voteStatus"/>:</th>
				<td>
					<s:if test="dicvoteStatus!=null">
						<s:select name="vote.voteStatus" list="dicvoteStatus" listKey="dictionaryKey" listValue="dictionaryValue" value="vote.voteStatus" cssClass="selectClass"/>
					</s:if>
				</td>
			</tr>
			<tr>
				<th><s:text name="vote.voteRepeatSubmit"/>:</th>
				<td>
					<s:if test="dicvoteRepeatSubmit!=null">
						<s:select name="vote.voteRepeatSubmit" list="dicvoteRepeatSubmit" listKey="dictionaryKey" listValue="dictionaryValue" value="vote.voteRepeatSubmit" cssClass="selectClass"/>
					</s:if>
				</td>
			</tr>
			<tr>
				<th><s:text name="vote.voteCheckType"/>:</th>
				<td>
					<s:if test="dicvoteCheckType!=null">
						<s:select name="vote.voteCheckType" list="dicvoteCheckType" listKey="dictionaryKey" listValue="dictionaryValue" value="vote.voteCheckType" cssClass="selectClass"/>
					</s:if>
				</td>
			</tr>
			<tr>
				<th><s:text name="vote.describe"/>:</th>
				<td><s:textarea name="vote.describe"  cssStyle="height:50px;overflow-y:visible;" required="true" requiredposition="right" cssClass="txtin"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.intervalTime"/>:</th>
				<td><s:textfield id="intervaTime" name="vote.intervalTime" required="true" requiredposition="right" maxlength="6" cssClass="txtin" onkeyup="this.value=this.value.replace(/\D/g,'')" onchange="this.value=this.value.replace(/\D/g,'')" />
					<span id="interPointMessage" style="color: red"></span><span style="color: #457d07">(<s:text name="hour"/>)</span>
				</td>
			</tr>
			<tr>
				<th><s:text name="vote.voteIpForbidden"/>:</th>
				<td><s:textarea name="vote.voteIpForbidden"  cssStyle="height:50px;overflow-y:visible;" required="true" requiredposition="right" cssClass="txtin"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.voteMacForbidden"/>:</th>
				<td><s:textarea name="vote.voteMacForbidden" cssStyle="height:50px;overflow-y:visible;" required="true" requiredposition="right" cssClass="txtin"/></td>
			</tr>
			<tr>
				<th rowspan="2"><s:text name="voteOption"/>:</th>
				<td><input  type="button" value="<s:text name="addVoteOption"/>" class="btnlist4" onclick="addOption()">&nbsp;<span style="color: red;">*</span></td>
			</tr>
			<tr>
				<td>
				<ul id="voteOpt">
				<s:if test="orig.equals(\"edit\")">
					<s:iterator value="optList">  
						<li>
							<s:hidden name="voteOptionId"/>
							<s:text name="option"/>:
							<s:textfield  name="voteOptionTitle" required="true" requiredposition="right" cssClass="txtin" onblur="checkOption();"/>
							<s:text name="ord"/>:
							<s:textfield id="vOrd" name="ord" required="true" requiredposition="right" cssClass="txtin" onblur="checkOrd();" onkeyup="this.value=this.value.replace(/\D/g,'')" onchange="this.value=this.value.replace(/\D/g,'')"/>
							<a href="#" class="del"><s:text name="delete"/></a>
							<span id="voteOptPointMessage" style="color: red;"></span>
						</li>
					</s:iterator>
					</s:if>
					<s:else>
						<li>
							<s:text name="option"/>:
							<s:textfield id="opt1" name="voteOptionTitle" required="true" requiredposition="right" cssClass="txtin" onblur="checkOption();"/>
							<s:text name="ord"/>:
							<s:textfield id="vOrd" name="ord" maxlength="6" required="true" value="1" requiredposition="right" cssClass="txtin" onblur="checkOrd();" onkeyup="this.value=this.value.replace(/\D/g,'')" onchange="this.value=this.value.replace(/\D/g,'')"/>
							<a href="#" class="del"><s:text name="delete"/></a>
							<span id="voteOptPointMessage"></span>
						</li>
						<li>
							<s:text name="option"/>:
							<s:textfield  id="opt2" name="voteOptionTitle" required="true" requiredposition="right" cssClass="txtin" onblur="checkOption();"/>
							<s:text name="ord"/>:
							<s:textfield id="vOrd" name="ord" maxlength="6" value="2" required="true" requiredposition="right" cssClass="txtin" onblur="checkOrd();" onkeyup="this.value=this.value.replace(/\D/g,'')" onchange="this.value=this.value.replace(/\D/g,'')"/>
							<a href="#this" class="del"><s:text name="delete"/></a>
							<span id="voteOptPointMessage"></span>
						</li>
						<li>
							<s:text name="option"/>:
							<s:textfield  id="opt3" name="voteOptionTitle" required="true" requiredposition="right" cssClass="txtin" onblur="checkOption();"/>
							<s:text name="ord"/>:
							<s:textfield id="vOrd" name="ord" maxlength="6" value="3" required="true" requiredposition="right" cssClass="txtin" onblur="checkOrd();" onkeyup="this.value=this.value.replace(/\D/g,'')" onchange="this.value=this.value.replace(/\D/g,'')"/>
							<a href="#this" class="del"><s:text name="delete"/></a>
							<span id="voteOptPointMessage"></span>
						</li>
						</s:else>
				</ul>
				</td>
			</tr>
			<s:if test="orig.equals(\"edit\")">
			<tr>
				<th><s:text name="vote.createRealName"/>:</th>
				<td><s:textfield name="vote.createRealName" required="true" requiredposition="right" cssClass="txtin unable" readonly="true"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.createLoginName"/>:</th>
				<td><s:textfield name="vote.createLoginName" required="true" requiredposition="right" cssClass="txtin unable" readonly="true"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.updateLoginName"/>:</th>
				<td><s:textfield name="vote.updateLoginName" required="true" requiredposition="right" cssClass="txtin unable"  readonly="true"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.updateRealName"/>:</th>
				<td><s:textfield name="vote.updateRealName" required="true" requiredposition="right" cssClass="txtin unable" readonly="true"/></td>
			</tr>
			<tr>
				<th><s:text name="vote.createTime"/>:</th>
				<td><s:textfield name="vote.createTime" required="true" requiredposition="right" cssClass="txtin unable"  readonly="true"><s:param name="value"><s:date name="vote.createTime" format="yyyy-MM-dd HH:mm:ss"/></s:param></s:textfield></td>
			</tr>
			<tr>
				<th><s:text name="vote.updateTime"/>:</th>
				<td><s:textfield name="vote.updateTime" required="true" requiredposition="right" cssClass="txtin unable" readonly="true"><s:param name="value"><s:date name="vote.updateTime" format="yyyy-MM-dd HH:mm:ss"/></s:param></s:textfield></td>
			</tr>
			
			<tr>
				<th><s:text name="vote.joinTotal"/>:</th>
				<td><s:textfield name="vote.joinTotal" required="true" requiredposition="right" cssClass="txtin unable" readonly="true"/></td>
			</tr>
			</s:if>
			<tr>
				<th>&nbsp;</th>
				<td>
					<s:if test="%{authLicence('2_2_5',#siteId)}">
						<s:submit value="%{getText('save')}" cssClass="btnlist2" id="infoBtn"/>
					</s:if>
					<s:else>
						<s:submit value="%{getText('save')}" disabled="true" cssClass="btnlist2"/>
					</s:else>
					<s:reset value="%{getText('返回')}" onclick="history.go(-1);" cssClass="btnlist2"/>
				</td>
			</tr>
		</table>
		</s:form>
	</div>
</div>
</body>
</html>
<script type="text/javascript" defer="defer">
	var voteId = $("#voteId").val();
	goDel();
	
	var isUni="no";
	function chkform(){
		getOption();
		var voteTitle = $("#voteTitle").val();
		var voteT = voteTitle.replace(/(^\s*)|(\s*$)/g, "");
		if(voteT == null || voteT == ""){
			$("#voteTitlePointMessage").css("color","red");
			$("#voteTitlePointMessage").html("抱歉 !投票主题不能为空");
			return false;
		}
		if(isUni == "have"){
			return false;
		}
		var opt1 = $("#opt1").val();
		var opt2 = $("#opt2").val();
		var opt3 = $("#opt3").val();
		var starttime=$("input[name=vote.startTime]").val().substr(0,10);
		var endtime=$("input[name=vote.endTime]").val().substr(0,10);
		if(starttime>endtime){
			$("#endtimeno").html("请选择合理的截至日期");
			$("#endtimeno").css("color","red");
			return false;
		}
		var cop = checkOption();
		if(!cop){
			return false;
		}
		var cor = checkOrd();
		if(!cor){
			return false;
		}
	}
	//输入1秒后检测用户名
	 var timer = false;
	 var fla = "no";
	 function check() {
	  	 if (timer) {
          clearTimeout(timer);
          timer = false;
	      }
	 	 timer = setTimeout(function () {
			   checkUniName();
		  }, 1000);
	}
	//进行名称唯一检测
		function checkUniName(){
			var votename = $("#votename").val();
			var voteTitle = $("#voteTitle").val();
			var obj = {"vote.voteTitle":voteTitle,"votename":votename};	
			if(voteTitle != null && voteTitle != ""){
			$.post("${ctx}/plugins/vote-checknameunique.action",obj,function(data){
				if(data == "ok"){
					$("#voteTitlePointMessage").css("color","#457d07");
					$("#voteTitlePointMessage").html("恭喜!投票主题名称可用");
					isUni="no";
				}else{
					$("#voteTitlePointMessage").css("color","red");
					$("#voteTitlePointMessage").html("抱歉!投票主题名称已经被占用，请重新输入");
					isUni="have";
					return false;
				}
			});
			}
		}
	function getOption(){
			var num = $("input[name='voteOptionTitle']").length;
			var optArray = new  Array(num);
			$("input[name='voteOptionTitle']").each(function(i){ 
				optArray[i]=$(this).attr("value");
			}); 
			$("#optarray").val(optArray);
			
			var onum = $("#input[name='ord']").length;
			var ordArray = new Array(onum);
			$("input[name='ord']").each(function(i){
				ordArray[i]=$(this).attr("value");
			});
			$("#ordArray").val(ordArray);
		if(voteId == null || voteId == ""){
		}else{
			var bnum = $("#input[name='voteOptionId']").length;
			var optIdArray = new Array(bnum);
			$("input[name='voteOptionId']").each(function(i){
				optIdArray[i]=$(this).attr("value");
			});
			$("#optIdArray").val(optIdArray);
		}
	}
	
	function addOption(){
		if(voteId == null || voteId == ""){
			var lastVal = $("#voteOpt").find("li:last").find("input:eq(1)").val();
			var lastval = Number(lastVal);
			$("#voteOptPointMessage").html("");
			$("#voteOpt").append($("#voteOpt").find("li:eq(0)").clone()); 
			$("#voteOpt").find("li:last input").each(function(){
				$("#voteOpt").find("li:last").find("input:eq(0)").val("");
				$("#voteOpt").find("li:last").find("input:eq(1)").val(lastval+1+"");
			});
		}else{
			var lastVal = $("#voteOpt").find("li:last").find("input:eq(2)").val();
			var lastval = Number(lastVal);
			$("#voteOptPointMessage").html("");
			$("#voteOpt").append($("#voteOpt").find("li:eq(0)").clone()); 
			$("#voteOpt").find("li:last input").each(function(){
				$("#voteOpt").find("li:last").find("input:eq(0)").val("");
				$("#voteOpt").find("li:last").find("input:eq(1)").val("");
				$("#voteOpt").find("li:last").find("input:eq(2)").val(lastval+1+"");
			});
		}
		goDel();
	}
	function resetOption(){
		if(voteId == null || voteId == ""){
		 $("#voteOpt").children().not($("#voteOpt").find("li:eq(0)")).remove();
		}else{
			$("input[name='voteOptionTitle']").val("");
			$("input[name='ord']").val("");
		}
	}
	function goDel(){
		$(".del").each(function(index){
			$(this).click(function(){
				var len = $("#voteOpt").find("li").length;
				
				if(len > 1){
					if(voteId != null || voteId != ""){
						var hidVal = $(this).siblings("input:eq(0)").val();
						if(hidVal != ""){
							del(index);
						}
					}
					$(this).parent().remove();
					$("#voteOpt").find("li").each(function(i,item){
						if(voteId != null || voteId != ""){
							$(item).find("input:eq(2)").val(i+1+"");
							$(item).find("input:eq(1)").attr("id","opt"+(i+1));
						}else{
							$(item).find("input:eq(1)").val(i+1+"");
							$(item).find("input:eq(0)").attr("id","opt"+(i+1));
						}
					});
				}else{
					$("#voteOptPointMessage").html("最后一个投票项不能删除！");
				}
			});
		});
	}
	function del(index){
		var bnum = $("#input[name='voteOptionId']").length;
		var optIdArray = new Array(bnum);
		$("input[name='voteOptionId']").each(function(i){
			optIdArray[i]=$(this).attr("value");
		});
		obj = {"key":optIdArray[index]};
		if(confirm("确实要删除吗？")){
		$.post("${ctx}/plugins/voteoption-del.action",obj,function(data){});
		}
	}
	function checkOption(){
		var flag = true;
		$("#voteOpt").find("li").each(function(i,item){
			if(voteId == null || voteId == ""){
				var option = $(item).children().first().val();
				if(option == ""){
					$(item).children().last().css("color","red");
					$(item).children().last().html("选项不能为空");
					$("#infoBtn").attr("disabled",true);
					flag = false;
				}else{
					$(item).children().last().html("");
					$("#infoBtn").attr("disabled",false);
				}
			}else{
				var option = $(item).children().first().next().val();
				if(option == ""){
					$(item).children().last().css("color","red");
					$(item).children().last().html("选项不能为空");
					$("#infoBtn").attr("disabled",true);
					flag = false;
				}else{
					$(item).children().last().html("");
					$("#infoBtn").attr("disabled",false);
				}
			}
		});
		return flag;
	}
	function checkOrd(){
		var flag = true;
		$("#voteOpt").find("li").each(function(i,item){
			if(voteId == null || voteId == ""){
				var ord = $(item).children().first().next().val();
				if(ord == ""){
					$(item).children().last().css("color","red");
					$(item).children().last().html("序号不能为空");
					$("#infoBtn").attr("disabled",true);
					flag = false;
				}else{
					$(item).children().last().html("");
					$("#infoBtn").attr("disabled",false);
				}
			}else{
				var ord = $(item).children().first().next().next().val();
				if(ord == ""){
					$(item).children().last().css("color","red");
					$(item).children().last().html("序号不能为空");
					$("#infoBtn").attr("disabled",true);
					flag = false;
				}else{
					$(item).children().last().html("");
					$("#infoBtn").attr("disabled",false);
				}
			}
		});
		return flag;
	}
	
</script>